package com.martin.lib_base.widget;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;

import com.martin.lib_base.R;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.widget.button.roundbutton.RoundDrawable;

/**
 * @author：Houge
 * @date：2022/8/10
 * @desc：
 */
public class RoundAppCompatTextView extends AppCompatTextView {
    public RoundAppCompatTextView(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public RoundAppCompatTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RoundAppCompatTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        RoundDrawable bg = fromAttributeSet(context, attrs);
        setBackgroundDrawable(bg);
    }

    public static RoundDrawable fromAttributeSet(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundAppCompatTextView);
        ColorStateList colorBg = ResUtils.getColorStateListAttrRes(context, typedArray, R.styleable.RoundAppCompatTextView_rtv_backgroundColor);
        ColorStateList colorBorder = ResUtils.getColorStateListAttrRes(context, typedArray, R.styleable.RoundAppCompatTextView_rtv_borderColor);
        int borderWidth = typedArray.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rtv_borderWidth, ResUtils.getDimensionPixelSize(context, R.dimen.default_radius));
        boolean isRadiusAdjustBounds = typedArray.getBoolean(R.styleable.RoundAppCompatTextView_rtv_isRadiusAdjustBounds, false);
        int mRadius = typedArray.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_ractv_radius, ResUtils.getDimensionPixelSize(context, R.dimen.default_radius));
        int mRadiusTopLeft = typedArray.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rtv_radiusTopLeft, 0);
        int mRadiusTopRight = typedArray.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rtv_radiusTopRight, 0);
        int mRadiusBottomLeft = typedArray.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rtv_radiusBottomLeft, 0);
        int mRadiusBottomRight = typedArray.getDimensionPixelSize(R.styleable.RoundAppCompatTextView_rtv_radiusBottomRight, 0);
        typedArray.recycle();

        RoundDrawable bg = new RoundDrawable();
        bg.setBgData(colorBg);
        bg.setStrokeData(borderWidth, colorBorder);
        bg.setIsRadiusAdjustBounds(isRadiusAdjustBounds);
        if (mRadiusTopLeft > 0 || mRadiusTopRight > 0 || mRadiusBottomLeft > 0 || mRadiusBottomRight > 0) {
            float[] radii = new float[]{
                    mRadiusTopLeft, mRadiusTopLeft,
                    mRadiusTopRight, mRadiusTopRight,
                    mRadiusBottomRight, mRadiusBottomRight,
                    mRadiusBottomLeft, mRadiusBottomLeft
            };
            bg.setCornerRadii(radii);
        } else {
            bg.setCornerRadius(mRadius);
        }
        return bg;
    }
}
